using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class LoadingPanel : MonoBehaviour
{
    // Start is called before the first frame update

    public RectTransform BG;
    public Color BackColor;

    public Disslove Card;

    public UnityEvent OnCover;
    public Sprite FrontImage;
    public Sprite BackImage;
    private void OnEnable()
    {
        StartCoroutine(Play());
    }

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    IEnumerator Play()
    {
        BG.DOScale(40, 1).From(0);
        BG.GetComponent<Image>().material.color = Color.black;
        Card.transform.DOScale(1, 1).From(0);
        Card.transform.DOScale(1, 0.7f).From(0.4f);
        Card.transform.DORotate(new Vector3(0,0,0), 1,RotateMode.FastBeyond360).From(new Vector3(0, 720, -30)).OnUpdate((() =>
        {
            // 获取Card的forward方向和摄像机的forward方向
            Vector3 cardForward = Card.transform.forward;
            Vector3 cameraForward = VisualCardsHandler.Instance.MainCam.transform.forward;

            // 计算两个方向向量的点积
            float dotProduct = Vector3.Dot(cardForward, cameraForward);

            //Debug.Log("Card与摄像机的forward方向的点积：" + dotProduct);
            if (dotProduct < 0)
            {
                Card.GetComponent<Image>().sprite = BackImage;
                Card.SetEffect(false);
            }
            else
            {
                Card.GetComponent<Image>().sprite = FrontImage;
                Card.SetEffect(true);

            }
        }));
        yield return new WaitForSeconds(0.5f);
        BG.GetComponent<Image>().material.DOColor(BackColor, 1).From(Color.black);
        yield return new WaitForSeconds(0.5f);
        OnCover?.Invoke();
        Card.Play(1, 0, 1f);
        
        BG.DOScale(0, 1).From(40);
        yield return new WaitForSeconds(1);
        Card.Play(1, 1, 0);
        gameObject.SetActive(false);
    }
}
